
<!DOCTYPE HTML>
<html lang="en" >
    <head>
        <meta charset="UTF-8">
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <title>2.1 核心接口与状态机 · smartboot 中文指南</title>
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="description" content="">
        <meta name="generator" content="GitBook 3.2.2">
        
        
        
    
    <link rel="stylesheet" href="../../../gitbook/style.css">

    
            
                
                <link rel="stylesheet" href="../../../gitbook/gitbook-plugin-search-plus/search.css">
                
            
                
                <link rel="stylesheet" href="../../../gitbook/gitbook-plugin-expandable-chapters-interactive/expandable-chapters.css">
                
            
                
                <link rel="stylesheet" href="../../../gitbook/gitbook-plugin-tbfed-pagefooter/footer.css">
                
            
                
                <link rel="stylesheet" href="../../../gitbook/gitbook-plugin-highlight/website.css">
                
            
                
                <link rel="stylesheet" href="../../../gitbook/gitbook-plugin-fontsettings/website.css">
                
            
        

    

    
        
    
        
    
        
    
        
    
        
    
        
    

        
    
    
    
    <meta name="HandheldFriendly" content="true"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../../../gitbook/images/apple-touch-icon-precomposed-152.png">
    <link rel="shortcut icon" href="../../../gitbook/images/favicon.ico" type="image/x-icon">

    
    <link rel="next" href="../服务端/" />
    
    
    <link rel="prev" href="../" />
    

    <style>
    @media only screen and (max-width: 640px) {
        .book-header .hidden-mobile {
            display: none;
        }
    }
    </style>
    <script>
        window["gitbook-plugin-github-buttons"] = {"buttons":[{"user":"smartboot","repo":"smart-socket","type":"star","size":"small","count":true}]};
    </script>

    </head>
    <body>
        
<div class="book">
    <div class="book-summary">
        
            
<div id="book-search-input" role="search">
    <input type="text" placeholder="Type to search" />
</div>

            
                <nav role="navigation">
                


<ul class="summary">
    
    

    

    
        
        
    
        <li class="chapter " data-level="1.1" data-path="../../../">
            
                <a href="../../../">
            
                    
                    关于smartboot
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2" >
            
                <span>
            
                    
                    smart-socket 使用手册
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.2.1" data-path="../../">
            
                <a href="../../">
            
                    
                    前言
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.2" >
            
                <span>
            
                    
                    第一章 smart-socket开发指南
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.2.2.1" data-path="../../chapter-1/2.1-基础应用/">
            
                <a href="../../chapter-1/2.1-基础应用/">
            
                    
                    1.1 快速上手
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.2.2" data-path="../../chapter-1/2.2-Spring集成/">
            
                <a href="../../chapter-1/2.2-Spring集成/">
            
                    
                    1.2 Spring集成smart-socket
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.2.3" data-path="../../chapter-1/SUMMARY.html">
            
                <a href="../../chapter-1/SUMMARY.html">
            
                    
                    小结
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.2.3" data-path="../">
            
                <a href="../">
            
                    
                    第二章 源码解析
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter active" data-level="1.2.3.1" data-path="./">
            
                <a href="./">
            
                    
                    2.1 核心接口与状态机
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.3.2" data-path="../服务端/">
            
                <a href="../服务端/">
            
                    
                    2.2 服务端
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.3.3" data-path="../客户端/">
            
                <a href="../客户端/">
            
                    
                    2.3 客户端
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.3.4" data-path="../AioSession/">
            
                <a href="../AioSession/">
            
                    
                    2.4 会话AioSession
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.3.5" data-path="../SUMMARY.html">
            
                <a href="../SUMMARY.html">
            
                    
                    2.5 小结
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.2.4" data-path="../../chapter-3/">
            
                <a href="../../chapter-3/">
            
                    
                    第三章 通信协议
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.2.4.1" data-path="../../chapter-3/1-消息归类/">
            
                <a href="../../chapter-3/1-消息归类/">
            
                    
                    3.1 消息归类
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.4.2" data-path="../../BLANK.html">
            
                <a href="../../BLANK.html">
            
                    
                    3.2 半包/粘包
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.4.3" data-path="../../BLANK.html">
            
                <a href="../../BLANK.html">
            
                    
                    3.3 Http消息解析
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.4.4" data-path="../../BLANK.html">
            
                <a href="../../BLANK.html">
            
                    
                    3.4 RPC消息解析
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.2.5" data-path="../../chapter-5/">
            
                <a href="../../chapter-5/">
            
                    
                    第四章 内存池
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.6" data-path="../../chapter-6/">
            
                <a href="../../chapter-6/">
            
                    
                    第五章 未来规划
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.7" data-path="../../end/">
            
                <a href="../../end/">
            
                    
                    附录
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.3" >
            
                <span>
            
                    
                    smart-http 实战手册
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.3.1" data-path="../../../smart-http/">
            
                <a href="../../../smart-http/">
            
                    
                    关于smart-http
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.2" >
            
                <span>
            
                    
                    第一章 smart-http开发指南
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.3.2.1" data-path="../../../smart-http/chapter-1/">
            
                <a href="../../../smart-http/chapter-1/">
            
                    
                    1.1 快速上手
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.2.2" data-path="../../../smart-http/chapter-1/http_route.html">
            
                <a href="../../../smart-http/chapter-1/http_route.html">
            
                    
                    1.2 请求路由
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.2.3" data-path="../../../smart-http/chapter-1/file_upload.html">
            
                <a href="../../../smart-http/chapter-1/file_upload.html">
            
                    
                    1.3 文件上传/下载
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.2.4" data-path="../../../smart-http/chapter-1/websocket.html">
            
                <a href="../../../smart-http/chapter-1/websocket.html">
            
                    
                    1.4 WebSocket
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.4" >
            
                <span>
            
                    
                    技术分享
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.4.1" data-path="../../../share/rpc/smart-socket-rpc.html">
            
                <a href="../../../share/rpc/smart-socket-rpc.html">
            
                    
                    smart-socket实现RPC
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.2" data-path="../../../share/firstfit/readme.html">
            
                <a href="../../../share/firstfit/readme.html">
            
                    
                    内存管理-首次适应算法
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.3" data-path="../../../share/thread/readme.html">
            
                <a href="../../../share/thread/readme.html">
            
                    
                    Thread知多少
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.5" data-path="../../../donation.html">
            
                <a href="../../../donation.html">
            
                    
                    关于捐赠
            
                </a>
            

            
        </li>
    

    

    <li class="divider"></li>

    <li>
        <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
            Published with GitBook
        </a>
    </li>
</ul>


                </nav>
            
        
    </div>

    <div class="book-body">
        
            <div class="body-inner">
                
                    

<div class="book-header" role="navigation">
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href="../../.." >2.1 核心接口与状态机</a>
    </h1>
</div>




                    <div class="page-wrapper" tabindex="-1" role="main">
                        <div class="page-inner">
                            
<div class="search-plus" id="book-search-results">
    <div class="search-noresults">
    
                                <section class="normal markdown-section">
                                
                                <h2 id="&#x6838;&#x5FC3;&#x63A5;&#x53E3;&#x4E0E;&#x72B6;&#x6001;&#x673A;">&#x6838;&#x5FC3;&#x63A5;&#x53E3;&#x4E0E;&#x72B6;&#x6001;&#x673A;</h2>
<p>&#x4E1A;&#x754C;&#x6709;&#x53E5;&#x8BDD;&#x53EB;&#x201C;&#x4E00;&#x6D41;&#x7684;&#x5356;&#x6807;&#x51C6;&#x3001;&#x4E8C;&#x6D41;&#x7684;&#x5356;&#x6280;&#x672F;&#x3001;&#x4E09;&#x6D41;&#x7684;&#x5356;&#x4EA7;&#x54C1;&#x201D;&#xFF0C;
&#x5982;&#x679C;&#x8BF4; smart-socket &#x7684;&#x6280;&#x672F;&#x4EF7;&#x503C;&#x4EC5;&#x7B97;&#x4E8C;&#x6D41;&#x6C34;&#x51C6;&#x7684;&#x8BDD;&#xFF0C;
&#x90A3;&#x4E48;&#x6211;&#x4EEC;&#x4E3A;&#x5176;&#x7CBE;&#x5FC3;&#x8BBE;&#x8BA1;&#x7684;&#x63A5;&#x53E3;&#x671F;&#x671B;&#x80FD;&#x7A0D;&#x5FAE;&#x63D0;&#x5347;&#x4E00;&#x4E0B;&#x5B83;&#x7684;&#x6863;&#x6B21;&#x3002;
&#x57FA;&#x4E8E; smart-socket &#x8FDB;&#x884C;&#x901A;&#x4FE1;&#x5F00;&#x53D1;&#x7684;&#x4E3B;&#x8981;&#x5DE5;&#x4F5C;&#x91CF;&#x57FA;&#x672C;&#x96C6;&#x4E2D;&#x5728;&#x4E24;&#x4E2A;&#x63A5;&#x53E3;&#x7684;&#x5B9E;&#x73B0;&#xFF1A;<code>Protocol</code>&#x3001;<code>MessageProcessor</code>&#x3002;</p>
<p><img src="core-api.png" alt=""></p>
<p>&#x4E24;&#x4E2A;&#x63A5;&#x53E3;&#x5206;&#x5DE5;&#x5F88;&#x660E;&#x786E;&#xFF0C;Protocol &#x8D1F;&#x8D23;&#x89E3;&#x6790;&#x6570;&#x636E;&#x5F62;&#x6210;&#x6D88;&#x606F;&#x5B9E;&#x4F53;&#xFF0C;&#x4E4B;&#x540E; smart-socket &#x4F1A;&#x628A;&#x8BE5;&#x6D88;&#x606F;&#x5B9E;&#x4F53;&#x4F20;&#x9012;&#x81F3; MessageProcessor &#x8FDB;&#x884C;&#x4E1A;&#x52A1;&#x5904;&#x7406;&#x3002;</p>
<p>&#x5F53;&#x7136;&#xFF0C;&#x4F60;&#x4E5F;&#x53EF;&#x4EE5;&#x5728; Protocol &#x4E2D;&#x4E00;&#x6B21;&#x6027;&#x5B8C;&#x6210;&#x89E3;&#x6790;&#x3001;&#x4E1A;&#x52A1;&#x5904;&#x7406;&#xFF0C;&#x53C8;&#x6216;&#x8005;&#x5C06; Protocol &#x5F53;&#x4E2A;&#x6446;&#x8BBE;&#xFF0C;&#x6240;&#x6709;&#x4E8B;&#x60C5;&#x96C6;&#x4E2D;&#x5728; MessageProcessor &#x5B8C;&#x6210;&#x3002;smart-socket &#x4E0D;&#x9650;&#x5236;&#x4F60;&#x5B9E;&#x73B0;&#x529F;&#x80FD;&#x7684;&#x81EA;&#x7531;&#x6027;&#xFF0C;&#x53EA;&#x662F;&#x63D0;&#x4F9B;&#x4E00;&#x4E2A;&#x66F4;&#x89C4;&#x8303;&#x3001;&#x66F4;&#x5408;&#x7406;&#x7684;&#x5EFA;&#x8BAE;&#xFF0C;&#x6700;&#x7EC8;&#x51B3;&#x5B9A;&#x6743;&#x8FD8;&#x662F;&#x5728;&#x7528;&#x6237;&#x7684;&#x624B;&#x4E2D;&#x3002;</p>
<h3 id="1-protocol">1. Protocol</h3>
<pre><code class="lang-java"><span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">interface</span> <span class="hljs-title">Protocol</span>&lt;<span class="hljs-title">T</span>&gt; </span>{
    <span class="hljs-function">T <span class="hljs-title">decode</span><span class="hljs-params">(<span class="hljs-keyword">final</span> ByteBuffer readBuffer, AioSession&lt;T&gt; session)</span></span>;
}
</code></pre>
<p>Protocol &#x662F;&#x4E00;&#x4E2A;&#x6CDB;&#x578B;&#x63A5;&#x53E3;&#xFF0C;<code>&lt;T&gt;</code>&#x6307;&#x7684;&#x662F;&#x4E1A;&#x52A1;&#x6D88;&#x606F;&#x5B9E;&#x4F53;&#x7C7B;&#xFF0C;smart-socket &#x4E2D;&#x4E0D;&#x5C11;&#x5730;&#x65B9;&#x90FD;&#x8FD0;&#x7528;&#x4E86;&#x6CDB;&#x578B;&#x8BBE;&#x8BA1;&#xFF0C;&#x5176;&#x542B;&#x4E49;&#x90FD;&#x4EE3;&#x8868;&#x6570;&#x636E;&#x89E3;&#x7801;&#x540E;&#x5C01;&#x88C5;&#x7684;&#x6D88;&#x606F;&#x7C7B;&#x578B;&#x3002;Protocol &#x4E2D;&#x53EA;&#x5B9A;&#x4E49;&#x4E86;&#x4E00;&#x4E2A;&#x65B9;&#x6CD5;<code>decode</code>&#x3002;</p>
<p>decode&#xFF08;&#x6D88;&#x606F;&#x89E3;&#x7801;&#xFF09;&#xFF0C;AIO &#x7684;&#x6570;&#x636E;&#x4F20;&#x8F93;&#x662F;&#x4EE5; ByteBuffer &#x4E3A;&#x5A92;&#x4ECB;&#x7684;&#x3002;&#x6240;&#x6709;&#x8BFB;&#x53D6;&#x5230;&#x7684;&#x5B57;&#x8282;&#x6570;&#x636E;&#x90FD;&#x4F1A;&#x586B;&#x5145;&#x5728; ByteBuffer &#x4E2D;&#x5E76;&#x4EE5;&#x4E8B;&#x4EF6;&#x56DE;&#x8C03;&#x7684;&#x5F62;&#x5F0F;&#x89E6;&#x53D1; Protocol#decode() &#x65B9;&#x6CD5;&#x3002;&#x6240;&#x4EE5;&#x6211;&#x4EEC;&#x5B9E;&#x73B0;&#x7684; decode &#x7B97;&#x6CD5;&#x5C31;&#x662F; ByteBuffer &#x5BF9;&#x8C61;&#x8F6C;&#x5316;&#x4E3A;&#x4E1A;&#x52A1;&#x6D88;&#x606F;<code>&lt;T&gt;</code>&#x7684;&#x8FC7;&#x7A0B;&#x3002;</p>
<p>&#x9700;&#x8981;&#x5F3A;&#x8C03;&#x4E00;&#x70B9;&#xFF0C;&#x8BFB;&#x8005;&#x670B;&#x53CB;&#x8BF7;&#x4E0D;&#x8981;&#x628A;&#x89E3;&#x7801;&#x60F3;&#x7684;&#x5F88;&#x7B80;&#x5355;&#xFF0C;&#x4EE4;&#x4EBA;&#x201C;&#x6DF1;&#x6076;&#x75DB;&#x7EDD;&#x201D;&#x7684;&#x534A;&#x5305;/&#x7C98;&#x5305;&#x5C31;&#x662F;&#x5728;&#x8FD9;&#x4E2A;&#x73AF;&#x8282;&#x9700;&#x8981;&#x5E94;&#x5BF9;&#x7684;&#x3002;&#x5904;&#x7406;&#x65B9;&#x5F0F;&#x4E5F;&#x4E0D;&#x96BE;&#xFF0C;<strong>&#x9075;&#x5B88;&#x4EE5;&#x4E0B;&#x4E24;&#x70B9;&#x5373;&#x53EF;</strong>&#xFF1A;</p>
<ol>
<li>&#x6839;&#x636E;&#x534F;&#x8BAE;&#x89E3;&#x6790;&#x6BCF;&#x4E00;&#x4E2A;&#x5B57;&#x6BB5;&#x524D;&#xFF0C;&#x90FD;&#x8981;&#x5148;&#x786E;&#x4FDD;&#x5269;&#x4F59;&#x6570;&#x636E;&#x6EE1;&#x8DB3;&#x89E3;&#x6790;&#x6240;&#x9700;&#xFF0C;&#x4E0D;&#x6EE1;&#x8DB3;&#x5219;&#x7EC8;&#x6B62;&#x8BE5;&#x5B57;&#x6BB5;&#x89E3;&#x6790;&#x5E76;&#x8FD4;&#x56DE; null&#x3002;</li>
<li>&#x5F53;&#x5DF2;&#x8BFB;&#x7684;&#x6570;&#x636E;&#x5DF2;&#x6EE1;&#x8DB3;&#x4E00;&#x4E2A;&#x5B8C;&#x6574;&#x4E1A;&#x52A1;&#x6D88;&#x606F;&#x6240;&#x9700;&#x65F6;&#xFF0C;&#x7ACB;&#x5373;&#x6784;&#x9020;&#x8BE5;&#x4E1A;&#x52A1;&#x5BF9;&#x8C61;&#x5E76;&#x8FD4;&#x56DE;&#xFF0C;&#x65E0;&#x9700;&#x5173;&#x5FC3; ByteBuffer &#x4E2D;&#x662F;&#x5426;&#x6709;&#x5269;&#x4F59;&#x7684;&#x6570;&#x636E;&#x3002;</li>
</ol>
<p>&#x8003;&#x8651;&#x5230;&#x6709;&#x4E9B;&#x8BFB;&#x8005;&#x5BF9;&#x4E0A;&#x8FF0;&#x4E24;&#x70B9;&#x8FD8;&#x4E0D;&#x751A;&#x7406;&#x89E3;&#xFF0C;&#x6211;&#x4EEC;&#x901A;&#x8FC7;&#x4E24;&#x4E2A;&#x793A;&#x4F8B;&#x6765;&#x6A21;&#x62DF;&#x901A;&#x4FE1;&#x8FC7;&#x7A0B;&#x4E2D;&#x7684;&#x534A;&#x5305;&#x3001;&#x7C98;&#x5305;&#x573A;&#x666F;&#x3002;&#x901A;&#x4FE1;&#x534F;&#x8BAE;&#x4F9D;&#x65E7;&#x662F;&#x5982;1.1&#x8282;&#x4E2D;&#x7684;&#x5B9A;&#x4E49;&#x7684;&#x7C7B;&#x578B;&#xFF1A;</p>
<p><img src="../../chapter-1/2.1-&#x57FA;&#x7840;&#x5E94;&#x7528;/2.1.2_1.png" alt="2.1.2_1"></p>
<ul>
<li><p>&#x534A;&#x5305;</p>
<pre><code class="lang-java"><span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">StringProtocol</span> <span class="hljs-keyword">implements</span> <span class="hljs-title">Protocol</span>&lt;<span class="hljs-title">String</span>&gt; </span>{
    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title">main</span><span class="hljs-params">(String[] args)</span> </span>{
        StringProtocol protocol = <span class="hljs-keyword">new</span> StringProtocol();
        <span class="hljs-keyword">byte</span>[] msgBody = <span class="hljs-string">&quot;smart-socket&quot;</span>.getBytes();
        <span class="hljs-keyword">byte</span> msgHead = (<span class="hljs-keyword">byte</span>) msgBody.length;
        System.out.println(<span class="hljs-string">&quot;&#x5B8C;&#x6574;&#x6D88;&#x606F;&#x957F;&#x5EA6;:&quot;</span> + (msgBody.length + <span class="hljs-number">1</span>));
        ByteBuffer buffer = ByteBuffer.allocate(msgBody.length);
        buffer.put(msgHead);
        buffer.put(msgBody, <span class="hljs-number">0</span>, buffer.remaining());
        buffer.flip();
        System.out.println(protocol.decode(buffer, <span class="hljs-keyword">null</span>));
    }

    <span class="hljs-function"><span class="hljs-keyword">public</span> String <span class="hljs-title">decode</span><span class="hljs-params">(ByteBuffer buffer, AioSession&lt;String&gt; session)</span> </span>{
        buffer.mark(); 
        <span class="hljs-keyword">byte</span> length = buffer.get(); 
        <span class="hljs-keyword">if</span> (buffer.remaining() &lt; length) { 
            System.out.println(<span class="hljs-string">&quot;&#x534A;&#x5305;&#xFF1A;&#x671F;&#x671B;&#x957F;&#x5EA6;:&quot;</span> + length + <span class="hljs-string">&quot; ,&#x5B9E;&#x9645;&#x5269;&#x4F59;&#x957F;&#x5EA6;:&quot;</span> + buffer.remaining());
            buffer.reset(); 
            <span class="hljs-keyword">return</span> <span class="hljs-keyword">null</span>;
        }
        <span class="hljs-keyword">byte</span>[] body = <span class="hljs-keyword">new</span> <span class="hljs-keyword">byte</span>[length];
        buffer.get(body); 
        buffer.mark(); 
        <span class="hljs-keyword">return</span> <span class="hljs-keyword">new</span> String(body); 
    }
}
</code></pre>
<p>&#x6839;&#x636E;&#x534F;&#x8BAE;&#x89C4;&#x5B9A;&#xFF0C;&#x5B8C;&#x6574;&#x7684;&#x6D88;&#x606F;&#x957F;&#x5EA6;&#x662F;&#x5B57;&#x7B26;&#x4E32;&#x201C;smart-socket&#x201D;&#x5B57;&#x8282;&#x6570;&#x52A0;&#x4E00;&#x4E2A;&#x5B57;&#x8282;&#x7684;&#x6D88;&#x606F;&#x5934;&#xFF0C;&#x5373;13&#x4F4D;&#x3002;&#x4F46;&#x56E0;&#x63A5;&#x6536;&#x6570;&#x636E;&#x7684; ByteBuffer &#x7A7A;&#x95F4;&#x4E0D;&#x8DB3;&#x5BFC;&#x81F4;&#x65E0;&#x6CD5;&#x5BB9;&#x7EB3;&#x6574;&#x4E2A;&#x6D88;&#x606F;&#xFF0C;&#x6B64;&#x65F6;&#x6267;&#x884C;&#x89E3;&#x7801;&#x7B97;&#x6CD5;<code>decode</code>&#x4FBF;&#x7B49;&#x540C;&#x4E8E;&#x901A;&#x4FE1;&#x4E2D;&#x7684;&#x534A;&#x5305;&#xFF0C;&#x8FD0;&#x884C;&#x540E;&#x63A7;&#x5236;&#x53F0;&#x6253;&#x5370;&#x5982;&#x4E0B;&#xFF1A;</p>
<pre><code>&#x5B8C;&#x6574;&#x6D88;&#x606F;&#x957F;&#x5EA6;:13
&#x534A;&#x5305;&#xFF1A;&#x671F;&#x671B;&#x957F;&#x5EA6;:12 ,&#x5B9E;&#x9645;&#x5269;&#x4F59;&#x957F;&#x5EA6;:11
null
</code></pre></li>
<li><p>&#x7C98;&#x5305;</p>
<pre><code class="lang-java"><span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">StringProtocol</span> <span class="hljs-keyword">implements</span> <span class="hljs-title">Protocol</span>&lt;<span class="hljs-title">String</span>&gt; </span>{
    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title">main</span><span class="hljs-params">(String[] args)</span> </span>{
        StringProtocol protocol = <span class="hljs-keyword">new</span> StringProtocol();
        <span class="hljs-keyword">byte</span>[] msgBody = <span class="hljs-string">&quot;smart-socket&quot;</span>.getBytes();
        <span class="hljs-keyword">byte</span> msgHead = (<span class="hljs-keyword">byte</span>) msgBody.length;
        System.out.println(<span class="hljs-string">&quot;&#x5B8C;&#x6574;&#x6D88;&#x606F;&#x957F;&#x5EA6;:&quot;</span> + (msgBody.length + <span class="hljs-number">1</span>));
        ByteBuffer buffer = ByteBuffer.allocate((msgBody.length + <span class="hljs-number">1</span>) * <span class="hljs-number">2</span>);
        <span class="hljs-comment">//&#x7B2C;&#x4E00;&#x4E2A;&#x6D88;&#x606F;</span>
        buffer.put(msgHead);
        buffer.put(msgBody);
        <span class="hljs-comment">//&#x7B2C;&#x4E8C;&#x4E2A;&#x6D88;&#x606F;</span>
        buffer.put(msgHead);
        buffer.put(msgBody);
        buffer.flip();
        String str = <span class="hljs-keyword">null</span>;
        <span class="hljs-keyword">while</span> ((str = protocol.decode(buffer, <span class="hljs-keyword">null</span>)) != <span class="hljs-keyword">null</span>) {
            System.out.println(<span class="hljs-string">&quot;&#x6D88;&#x606F;&#x89E3;&#x7801;&#x6210;&#x529F;:&quot;</span>+str);
        }
    }

    <span class="hljs-function"><span class="hljs-keyword">public</span> String <span class="hljs-title">decode</span><span class="hljs-params">(ByteBuffer buffer, AioSession&lt;String&gt; session)</span> </span>{
        <span class="hljs-keyword">if</span> (!buffer.hasRemaining()) {
            <span class="hljs-keyword">return</span> <span class="hljs-keyword">null</span>;
        }
        buffer.mark();
        <span class="hljs-keyword">byte</span> length = buffer.get();
        <span class="hljs-keyword">if</span> (buffer.remaining() &lt; length) {
            System.out.println(<span class="hljs-string">&quot;&#x534A;&#x5305;&#xFF1A;&#x671F;&#x671B;&#x957F;&#x5EA6;:&quot;</span> + length + <span class="hljs-string">&quot; ,&#x5B9E;&#x9645;&#x5269;&#x4F59;&#x957F;&#x5EA6;:&quot;</span> + buffer.remaining());
            buffer.reset();
            <span class="hljs-keyword">return</span> <span class="hljs-keyword">null</span>;
        }
        <span class="hljs-keyword">byte</span>[] body = <span class="hljs-keyword">new</span> <span class="hljs-keyword">byte</span>[length];
        buffer.get(body);
        buffer.mark();
        <span class="hljs-keyword">return</span> <span class="hljs-keyword">new</span> String(body);
    }
}
</code></pre>
<p>&#x7C98;&#x5305;&#x51FA;&#x73B0;&#x4E8E;&#x5DF2;&#x8BFB;&#x6570;&#x636E;&#x7684;&#x90E8;&#x5206;&#x8D85;&#x8FC7;&#x4E86;&#x4E00;&#x4E2A;&#x5B8C;&#x6574;&#x7684;&#x6D88;&#x606F;&#x957F;&#x5EA6;&#x3002;&#x5982; demo &#x6240;&#x793A;&#xFF0C;&#x6211;&#x4EEC;&#x5728; ByteBuffer &#x4E2D;&#x653E;&#x5165;&#x4E86;&#x7B26;&#x5408;&#x534F;&#x8BAE;&#x8D35;&#x7684;&#x4E24;&#x4E2A;&#x5B8C;&#x6574;&#x6D88;&#x606F;&#xFF0C;&#x6309;&#x7167;&#x89E3;&#x7801;&#x7B97;&#x6CD5;&#x89E3;&#x6790;&#x51FA;&#x7B2C;&#x4E00;&#x4E2A;&#x6D88;&#x606F;&#x91CC;&#x7ACB;&#x5373;&#x8FD4;&#x56DE;<code>new String(body)</code>&#xFF0C;&#x5F85;&#x8BE5;&#x6D88;&#x606F;&#x5904;&#x7406;&#x5B8C;&#x6210;&#x540E;&#x518D;&#x8FDB;&#x884C;&#x4E0B;&#x4E00;&#x6B21;&#x89E3;&#x7801;&#x3002;&#x6545;&#x4E0A;&#x8FF0;&#x4F8B;&#x5B50;&#x7684;&#x63A7;&#x5236;&#x53F0;&#x6253;&#x5370;&#x5982;&#x4E0B;&#xFF1A;</p>
<pre><code>&#x5B8C;&#x6574;&#x6D88;&#x606F;&#x957F;&#x5EA6;:13
&#x6D88;&#x606F;&#x89E3;&#x7801;&#x6210;&#x529F;:smart-socket
&#x6D88;&#x606F;&#x89E3;&#x7801;&#x6210;&#x529F;:smart-socket
</code></pre></li>
</ul>
<p>&#x81F3;&#x6B64;&#x6211;&#x4EEC;&#x5DF2;&#x7ECF;&#x4E3A;&#x5927;&#x5BB6;&#x4ECB;&#x7ECD;&#x4E86; Protocol &#x7684;&#x7279;&#x6027;&#x4EE5;&#x53CA;&#x5BF9;&#x4E8E;&#x534A;&#x5305;&#x7C98;&#x5305;&#x7684;&#x5904;&#x7406;&#x65B9;&#x5F0F;&#xFF0C;&#x5F53;&#x7136;&#x771F;&#x5B9E;&#x573A;&#x666F;&#x4E0B;&#x6211;&#x4EEC;&#x4F1A;&#x9762;&#x4E34;&#x66F4;&#x590D;&#x6742;&#x7684;&#x534F;&#x8BAE;&#xFF0C;&#x5BF9;&#x4E8E;&#x534A;&#x5305;&#x7C98;&#x5305;&#x7684;&#x5904;&#x7406;&#x65B9;&#x5F0F;&#x4E5F;&#x662F;&#x591A;&#x79CD;&#x591A;&#x6837;&#xFF0C;&#x5728;&#x901A;&#x4FE1;&#x534F;&#x8BAE;&#x7AE0;&#x8282;&#x5728;&#x8BE6;&#x7EC6;&#x8BF4;&#x660E;&#x3002;</p>
<h3 id="2-messageprocessor">2. MessageProcessor</h3>
<pre><code class="lang-java"><span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">interface</span> <span class="hljs-title">MessageProcessor</span>&lt;<span class="hljs-title">T</span>&gt; </span>{

    <span class="hljs-comment">/**
     * &#x5904;&#x7406;&#x63A5;&#x6536;&#x5230;&#x7684;&#x6D88;&#x606F;
     *
     * <span class="hljs-doctag">@param</span> session &#x901A;&#x4FE1;&#x4F1A;&#x8BDD;
     * <span class="hljs-doctag">@param</span> msg     &#x5F85;&#x5904;&#x7406;&#x7684;&#x4E1A;&#x52A1;&#x6D88;&#x606F;
     */</span>
    <span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">process</span><span class="hljs-params">(AioSession&lt;T&gt; session, T msg)</span></span>;

    <span class="hljs-comment">/**
     * &#x72B6;&#x6001;&#x673A;&#x4E8B;&#x4EF6;,&#x5F53;&#x679A;&#x4E3E;&#x4E8B;&#x4EF6;&#x53D1;&#x751F;&#x65F6;&#x7531;&#x6846;&#x67B6;&#x89E6;&#x53D1;&#x8BE5;&#x65B9;&#x6CD5;
     *
     *
     * <span class="hljs-doctag">@param</span> session          &#x672C;&#x6B21;&#x89E6;&#x53D1;&#x72B6;&#x6001;&#x673A;&#x7684;AioSession&#x5BF9;&#x8C61;
     * <span class="hljs-doctag">@param</span> stateMachineEnum &#x72B6;&#x6001;&#x679A;&#x4E3E;
     * <span class="hljs-doctag">@param</span> throwable        &#x5F02;&#x5E38;&#x5BF9;&#x8C61;&#xFF0C;&#x5982;&#x679C;&#x5B58;&#x5728;&#x7684;&#x8BDD;
     * <span class="hljs-doctag">@see</span> StateMachineEnum
     */</span>
    <span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">stateEvent</span><span class="hljs-params">(AioSession&lt;T&gt; session, StateMachineEnum stateMachineEnum, Throwable throwable)</span></span>;
}
</code></pre>
<p>Protocol &#x4FA7;&#x91CD;&#x4E8E;&#x901A;&#x4FE1;&#x5C42;&#x7684;&#x6570;&#x636E;&#x89E3;&#x6790;&#xFF0C;&#x800C; MessageProcessor &#x5219;&#x8D1F;&#x8D23;&#x5E94;&#x7528;&#x5C42;&#x7684;&#x6D88;&#x606F;&#x4E1A;&#x52A1;&#x5904;&#x7406;&#x3002;&#x5B9A;&#x4E49;&#x4E86;&#x6D88;&#x606F;&#x5904;&#x7406;&#x5668;&#x63A5;&#x53E3;&#xFF0C;smart-socket &#x5728;&#x901A;&#x8FC7; Protocol &#x5B8C;&#x6210;&#x6D88;&#x606F;&#x89E3;&#x7801;&#x540E;&#xFF0C;&#x4F1A;&#x5C06;&#x6D88;&#x606F;&#x5BF9;&#x8C61;&#x4EA4;&#x7531; MessageProcessor &#x5B9E;&#x73B0;&#x7C7B;&#x8FDB;&#x884C;&#x4E1A;&#x52A1;&#x5904;&#x7406;&#x3002;</p>
<ul>
<li>process
&#x6D88;&#x606F;&#x5904;&#x7406;&#x5668;&#xFF0C;smart-socket &#x6BCF;&#x63A5;&#x6536;&#x5230;&#x4E00;&#x4E2A;&#x5B8C;&#x6574;&#x7684;&#x4E1A;&#x52A1;&#x6D88;&#x606F;&#xFF0C;&#x90FD;&#x4F1A;&#x4EA4;&#x7531;&#x8BE5;&#x5904;&#x7406;&#x5668;&#x6267;&#x884C;&#x3002;</li>
<li>stateEvent
&#x6267;&#x884C;&#x72B6;&#x6001;&#x673A;&#xFF0C;smart-socket &#x5185;&#x7F6E;&#x4E86;&#x72B6;&#x6001;&#x679A;&#x4E3E;<code>StateMachineEnum</code>&#x3002;<code>MessageProcessor</code>&#x5B9E;&#x73B0;&#x7C7B;&#x53EF;&#x5728;&#x6B64;&#x65B9;&#x6CD5;&#x4E2D;&#x5904;&#x7406;&#x5176;&#x5173;&#x6CE8;&#x7684;&#x4E8B;&#x4EF6;&#x3002;</li>
</ul>
<h3 id="3-&#x72B6;&#x6001;&#x673A;statemachineenum">3. &#x72B6;&#x6001;&#x673A;StateMachineEnum</h3>
<p>smart-socket&#x4E2D; &#x5F15;&#x5165;&#x4E86;&#x72B6;&#x6001;&#x673A;&#x7684;&#x6982;&#x5FF5;&#xFF0C;&#x72B6;&#x6001;&#x673A;&#x7684;&#x5B58;&#x5728;&#x4E0D;&#x4F1A;&#x51B3;&#x7B56; smart-socket &#x5BF9;&#x4E8E;&#x901A;&#x4FE1;&#x7684;&#x4E8B;&#x4EF6;&#x5904;&#x7406;&#xFF0C;&#x4F46;&#x4F1A;&#x5728;&#x7279;&#x5B9A;&#x4E8B;&#x4EF6;&#x53D1;&#x751F;&#x4E4B;&#x65F6;&#x901A;&#x77E5;&#x6D88;&#x606F;&#x5904;&#x7406;&#x5668;<code>MessageProcessor#stateEvent</code>&#x3002;&#x76EE;&#x524D;&#x5DF2;&#x6709;&#x7684;&#x72B6;&#x6001;&#x679A;&#x4E3E;&#x4E3A;&#xFF1A;</p>
<table>
<thead>
<tr>
<th>&#x72B6;&#x6001;&#x679A;&#x4E3E;</th>
<th>&#x8BF4;&#x660E;</th>
</tr>
</thead>
<tbody>
<tr>
<td>NEW_SESSION</td>
<td>&#x7F51;&#x7EDC;&#x8FDE;&#x63A5;&#x5EFA;&#x7ACB;&#x65F6;&#x89E6;&#x53D1;&#xFF0C;&#x8FDE;&#x63A5;&#x5EFA;&#x7ACB;&#x65F6;&#x4F1A;&#x6784;&#x5EFA;&#x4F20;&#x8F93;&#x5C42;&#x7684;AioSession&#xFF0C;&#x5982;&#x679C;&#x4E1A;&#x52A1;&#x5C42;&#x9762;&#x4E5F;&#x9700;&#x8981;&#x7EF4;&#x62A4;&#x4E00;&#x4E2A;&#x4F1A;&#x8BDD;&#xFF0C;&#x53EF;&#x5728;&#x6B64;&#x72B6;&#x6001;&#x673A;&#x4E2D;&#x5904;&#x7406;</td>
</tr>
<tr>
<td>INPUT_SHUTDOWN</td>
<td>&#x6570;&#x636E;&#x8BFB;&#x53D6;&#x5B8C;&#x6BD5;&#x65F6;&#x89E6;&#x53D1;&#xFF0C;&#x5373;&#x4F20;&#x7EDF;&#x610F;&#x4E49;&#x4E2D;&#x7684;<code>read()==-1</code></td>
</tr>
<tr>
<td>INPUT_EXCEPTION</td>
<td>&#x8BFB;&#x6570;&#x636E;&#x8FC7;&#x7A0B;&#x4E2D;&#x53D1;&#x751F;&#x5F02;&#x5E38;&#x65F6;&#x89E6;&#x53D1;&#x6B64;&#x72B6;&#x6001;&#x673A;</td>
</tr>
<tr>
<td>OUTPUT_EXCEPTION</td>
<td>&#x5199;&#x6570;&#x636E;&#x8FC7;&#x7A0B;&#x4E2D;&#x53D1;&#x751F;&#x5F02;&#x5E38;&#x65F6;&#x89E6;&#x53D1;&#x6B64;&#x72B6;&#x6001;&#x673A;</td>
</tr>
<tr>
<td>SESSION_CLOSING</td>
<td>&#x89E6;&#x53D1;&#x4E86;AioSession.close&#x65B9;&#x6CD5;&#xFF0C;&#x4F46;&#x7531;&#x4E8E;&#x5F53;&#x524D;AioSession&#x8FD8;&#x6709;&#x672A;&#x5B8C;&#x6210;&#x7684;&#x4E8B;&#x4EF6;&#xFF0C;&#x4F1A;&#x8FDB;&#x5165;SESSION_CLOSING&#x72B6;&#x6001;</td>
</tr>
<tr>
<td>SESSION_CLOSED</td>
<td>AioSesson&#x5B8C;&#x6210;close&#x64CD;&#x4F5C;&#x540E;&#x89E6;&#x53D1;&#x6B64;&#x72B6;&#x6001;&#x673A;</td>
</tr>
<tr>
<td>PROCESS_EXCEPTION</td>
<td>&#x4E1A;&#x52A1;&#x5904;&#x7406;&#x5F02;&#x5E38;</td>
</tr>
<tr>
<td>DECODE_EXCEPTION</td>
<td>&#x89E3;&#x7801;&#x5F02;&#x5E38;</td>
</tr>
<tr>
<td>REJECT_ACCEPT</td>
<td>&#x670D;&#x52A1;&#x7AEF;&#x62D2;&#x7EDD;&#x5BA2;&#x6237;&#x7AEF;&#x8FDE;&#x63A5;&#x8BF7;&#x6C42;</td>
</tr>
</tbody>
</table>
<p>&#x72B6;&#x6001;&#x673A;&#x4F34;&#x8D2F;&#x7A7F;&#x4E86;&#x901A;&#x4FE1;&#x670D;&#x52A1;&#x7684;&#x6574;&#x4E2A;&#x751F;&#x547D;&#x5468;&#x671F;&#xFF0C;&#x5728;&#x8FD9;&#x4E2A;&#x8FC7;&#x7A0B;&#x4E2D;&#x4E0D;&#x540C;&#x4E8B;&#x4EF6;&#x7684;&#x53D1;&#x751F;&#x4F1A;&#x89E6;&#x53D1;&#x4E0D;&#x540C;&#x7684;&#x72B6;&#x6001;&#x673A;&#x3002;&#x901A;&#x4FE1;&#x4E8B;&#x4EF6;&#x4E0E;&#x72B6;&#x6001;&#x673A;&#x7684;&#x5173;&#x7CFB;&#x5982;&#x4E0B;&#x56FE;&#x6240;&#x793A;&#x3002;</p>
<p><img src="2.2.2_1.png" width="90%"></p>
<center>&#x56FE;2.2.2</center>

<p>&#x72B6;&#x6001;&#x673A;&#x76F8;&#x5BF9;&#x4E8E;&#x6574;&#x4E2A;&#x901A;&#x4FE1;&#x73AF;&#x5883;&#x7684;&#x5404;&#x4E2A;&#x8282;&#x70B9;&#x53EA;&#x662F;&#x4E00;&#x4E2A;&#x65C1;&#x89C2;&#x8005;&#xFF0C;&#x5B83;&#x89C1;&#x8BC1;&#x4E86;&#x5404;&#x4E2A;&#x4E8B;&#x4EF6;&#x7684;&#x53D1;&#x751F;&#xFF0C;&#x5374;&#x65E0;&#x529B;&#x626D;&#x8F6C;&#x4E8B;&#x4EF6;&#x7684;&#x53D1;&#x5C55;&#x65B9;&#x5411;&#x3002;&#x72B6;&#x6001;&#x673A;&#x672C;&#x8D28;&#x5176;&#x5B9E;&#x8DDF;&#x5927;&#x5BB6;&#x6240;&#x8BA4;&#x77E5;&#x7684;&#x8FC7;&#x6EE4;&#x5668;&#x3001;&#x62E6;&#x622A;&#x5668;&#x6709;&#x70B9;&#x7C7B;&#x4F3C;&#xFF0C;&#x90A3;&#x4E3A;&#x4EC0;&#x4E48;smart-socket&#x8981;&#x5982;&#x6B64;&#x8BBE;&#x8BA1;&#x5462;&#xFF1F;&#x60F3;&#x60F3;&#x4E00;&#x4E0B;&#x5982;&#x679C;&#x6211;&#x4EEC;&#x6309;&#x7167;&#x8FC7;&#x6EE4;&#x5668;&#x7684;&#x8BBE;&#x8BA1;&#x601D;&#x8DEF;&#xFF0C;&#x5176;&#x5F62;&#x6001;&#x4F1A;&#x5982;&#x4E0B;&#x6240;&#x793A;&#xFF1A;</p>
<pre><code class="lang-java"><span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">interface</span> <span class="hljs-title">Filter</span></span>{
    <span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">newSession</span><span class="hljs-params">(AioSesion session)</span></span>;
    <span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">processException</span><span class="hljs-params">(AioSession session,Throwable throwable)</span></span>;
    <span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">decodeExcepton</span><span class="hljs-params">(AioSession session,Throwable throwable)</span></span>;
    <span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">inputException</span><span class="hljs-params">(AioSession session,Throwable throwable)</span></span>;
    <span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">outputException</span><span class="hljs-params">(AioSession session,Throwable throwable)</span></span>;
    <span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">sessionClosing</span><span class="hljs-params">(AioSession session)</span></span>;
    <span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">sessionClosed</span><span class="hljs-params">(AioSession session)</span></span>;
}
</code></pre>
<p>&#x8FD9;&#x6837;&#x7684;&#x8BBE;&#x8BA1;&#x5B58;&#x5728;&#x4EE5;&#x4E0B;&#x7F3A;&#x9677;&#xFF1A;</p>
<ol>
<li>&#x5BF9;&#x5B9E;&#x73B0;&#x7C7B;&#x4E0D;&#x53CB;&#x597D;&#xFF1B;&#x4E5F;&#x8BB8;&#x6211;&#x53EA;&#x60F3;&#x5904;&#x7406; newSession&#xFF0C;&#x5374;&#x4E0D;&#x5F97;&#x4E0D;&#x4FDD;&#x7559;&#x5176;&#x4F59;&#x65B9;&#x6CD5;&#x7684;&#x7A7A;&#x5B9E;&#x73B0;&#xFF1B;</li>
<li>&#x65E0;&#x6CD5;&#x5E73;&#x6ED1;&#x5347;&#x7EA7;&#xFF1B;&#x52A0;&#x5165;&#x65B0;&#x7248;&#x672C;&#x4E2D;&#x52A0;&#x5165;&#x65B0;&#x7684;&#x4E8B;&#x4EF6;&#x7C7B;&#x578B;&#xFF0C;&#x8001;&#x7248;&#x672C;&#x4EE3;&#x7801;&#x9700;&#x8981;&#x5168;&#x90E8;&#x66F4;&#x6539;&#xFF1B;</li>
</ol>
<p>&#x800C;&#x91C7;&#x7528;&#x72B6;&#x6001;&#x673A;&#x6A21;&#x5F0F;&#xFF0C;&#x4E0D;&#x4EC5;&#x89E3;&#x51B3;&#x4E86;&#x4E0A;&#x8FF0;&#x95EE;&#x9898;&#xFF0C;&#x8FD8;&#x4E3A;&#x901A;&#x4FE1;&#x670D;&#x52A1;&#x7684;&#x591A;&#x5143;&#x5316;&#x6269;&#x5C55;&#x5E26;&#x4E86;&#x4FBF;&#x5229;&#x3002;
&#x4F8B;&#x5982; IM &#x573A;&#x666F;&#x4E0B;&#xFF0C;&#x6211;&#x4EEC;&#x5728; NEW_SESSION &#x72B6;&#x6001;&#x673A;&#x4E2D;&#x6536;&#x96C6; Session &#x96C6;&#x5408;&#xFF0C;&#x5728;&#x6D88;&#x606F;&#x5904;&#x7406;&#x65F6;&#x5F88;&#x5BB9;&#x6613;&#x5C31;&#x80FD;&#x5B9E;&#x73B0;&#x6D88;&#x606F;&#x7FA4;&#x53D1;&#xFF1B;
&#x5F53;&#x67D0;&#x4E2A;&#x7528;&#x6237;&#x65AD;&#x7EBF;&#x540E;&#xFF0C;&#x6211;&#x4EEC;&#x53CA;&#x65F6;&#x5728;&#x72B6;&#x6001;&#x673A; SESSION_CLOSED &#x4E2D;&#x611F;&#x77E5;&#x5230;&#x5E76;&#x66F4;&#x65B0; Session &#x96C6;&#x5408;&#x4E2D;&#x7684;&#x4F1A;&#x8BDD;&#x72B6;&#x6001;&#xFF0C;&#x751A;&#x81F3;&#x53EF;&#x4EE5;&#x7FA4;&#x53D1;&#x6D88;&#x606F;&#x7ED9;&#x6240;&#x6709;&#x7528;&#x6237;&#x201C;&#x67D0;&#x67D0;&#x4EBA;&#x6389;&#x7EBF;&#x4E86;&#x201D;&#x3002;&#x8FD9;&#x4E9B;&#x901A;&#x4FE1;&#x72B6;&#x6001;&#x548C;&#x4E1A;&#x52A1;&#x76F8;&#x7ED3;&#x5408;&#x7684;&#x573A;&#x666F;&#xFF0C; &#x7528;&#x72B6;&#x6001;&#x673A;&#x80FD;&#x5F88;&#x597D;&#x7684;&#x5F97;&#x4EE5;&#x89E3;&#x51B3;&#x3002;&#x6700;&#x540E;&#x5949;&#x4E0A;&#x4E00;&#x6BB5;&#x7C97;&#x7CD9;&#x7684;&#x4F2A;&#x4EE3;&#x7801;&#xFF0C;&#x8BFB;&#x8005;&#x81EA;&#x884C;&#x9886;&#x609F;&#x3002;</p>
<pre><code class="lang-java"><span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">IMProcessor</span> <span class="hljs-keyword">implements</span> <span class="hljs-title">MessageProcessor</span>&lt;<span class="hljs-title">Message</span>&gt; </span>{
    <span class="hljs-keyword">private</span> LinkedBlockingQueue sessions = <span class="hljs-keyword">new</span> LinkedBlockingQueue();

    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">process</span><span class="hljs-params">(AioSession&lt;String&gt; session, Message msg)</span> </span>{
        <span class="hljs-keyword">for</span>(AioSession otherSession:sessions){
            <span class="hljs-keyword">if</span>(otherSession==session){
                <span class="hljs-keyword">continue</span>;
            }
            sendMessage(otherSession,session+<span class="hljs-string">&quot;&#x7FA4;&#x53D1;&#x9001;&#x6D88;&#x606F;&#xFF1A;&quot;</span>+msg);
        }
    }

    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">stateEvent</span><span class="hljs-params">(AioSession&lt;Message&gt; session, StateMachineEnum state, Throwable throwable)</span> </span>{
        <span class="hljs-keyword">switch</span> (state) {
            <span class="hljs-keyword">case</span> NEW_SESSION:
                sessions.add(session);
                <span class="hljs-keyword">break</span>;
            <span class="hljs-keyword">case</span> SESSION_CLOSED:
                sessions.remove(session);
                <span class="hljs-keyword">break</span>;
        }
    }
}
</code></pre>
<footer class="page-footer"><span class="copyright">Copyright &#xA9; smartboot.org 2018 all right reserved&#xFF0C;powered by Gitbook</span><span class="footer-modification">&#x8BE5;&#x6587;&#x4EF6;&#x4FEE;&#x8BA2;&#x65F6;&#x95F4;&#xFF1A;
2019-11-21 21:08:55
</span></footer>
                                
                                </section>
                            
    </div>
    <div class="search-results">
        <div class="has-results">
            
            <h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
            <ul class="search-results-list"></ul>
            
        </div>
        <div class="no-results">
            
            <h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
            
        </div>
    </div>
</div>

                        </div>
                    </div>
                
            </div>

            
                
                <a href="../" class="navigation navigation-prev " aria-label="Previous page: 第二章 源码解析">
                    <i class="fa fa-angle-left"></i>
                </a>
                
                
                <a href="../服务端/" class="navigation navigation-next " aria-label="Next page: 2.2 服务端">
                    <i class="fa fa-angle-right"></i>
                </a>
                
            
        
    </div>

    <script>
        var gitbook = gitbook || [];
        gitbook.push(function() {
            gitbook.page.hasChanged({"page":{"title":"2.1 核心接口与状态机","level":"1.2.3.1","depth":3,"next":{"title":"2.2 服务端","level":"1.2.3.2","depth":3,"path":"smart-socket/chapter-2/服务端/README.md","ref":"smart-socket/chapter-2/服务端/README.md","articles":[]},"previous":{"title":"第二章 源码解析","level":"1.2.3","depth":2,"path":"smart-socket/chapter-2/README.md","ref":"smart-socket/chapter-2/README.md","articles":[{"title":"2.1 核心接口与状态机","level":"1.2.3.1","depth":3,"path":"smart-socket/chapter-2/核心接口/README.md","ref":"smart-socket/chapter-2/核心接口/README.md","articles":[]},{"title":"2.2 服务端","level":"1.2.3.2","depth":3,"path":"smart-socket/chapter-2/服务端/README.md","ref":"smart-socket/chapter-2/服务端/README.md","articles":[]},{"title":"2.3 客户端","level":"1.2.3.3","depth":3,"path":"smart-socket/chapter-2/客户端/README.md","ref":"smart-socket/chapter-2/客户端/README.md","articles":[]},{"title":"2.4 会话AioSession","level":"1.2.3.4","depth":3,"path":"smart-socket/chapter-2/AioSession/README.md","ref":"smart-socket/chapter-2/AioSession/README.md","articles":[]},{"title":"2.5 小结","level":"1.2.3.5","depth":3,"path":"smart-socket/chapter-2/SUMMARY.md","ref":"smart-socket/chapter-2/SUMMARY.md","articles":[]}]},"dir":"ltr"},"config":{"plugins":["-lunr","-search","-sharing","search-plus","expandable-chapters-interactive","tbfed-pagefooter","github","github-buttons","edit-link"],"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"pluginsConfig":{"tbfed-pagefooter":{"copyright":"Copyright &copy smartboot.org 2018","modify_label":"该文件修订时间：","modify_format":"YYYY-MM-DD HH:mm:ss"},"github":{"url":"https://github.com/smartboot/smart-socket"},"fontsettings":{"theme":"white","family":"sans","size":2},"highlight":{},"expandable-chapters-interactive":{},"github-buttons":{"buttons":[{"user":"smartboot","repo":"smart-socket","type":"star","size":"small","count":true}]},"edit-link":{"label":"Edit This Page","base":"https://gitee.com/smartboot/book/edit/master"},"theme-default":{"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"showLevel":false},"search-plus":{}},"theme":"default","pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"variables":{},"title":"smartboot 中文指南","language":"en","gitbook":"*","description":"本文涵盖了smartboot下所有项目的技术手册"},"file":{"path":"smart-socket/chapter-2/核心接口/README.md","mtime":"2019-11-21T13:08:55.796Z","type":"markdown"},"gitbook":{"version":"3.2.2","time":"2020-04-02T05:22:17.739Z"},"basePath":"../../..","book":{"language":""}});
        });
    </script>
</div>

        
    <script src="../../../gitbook/gitbook.js"></script>
    <script src="../../../gitbook/theme.js"></script>
    
        
        <script src="../../../gitbook/gitbook-plugin-search-plus/jquery.mark.min.js"></script>
        
    
        
        <script src="../../../gitbook/gitbook-plugin-search-plus/search.js"></script>
        
    
        
        <script src="../../../gitbook/gitbook-plugin-expandable-chapters-interactive/expandable-chapters.js"></script>
        
    
        
        <script src="../../../gitbook/gitbook-plugin-github/plugin.js"></script>
        
    
        
        <script src="../../../gitbook/gitbook-plugin-github-buttons/plugin.js"></script>
        
    
        
        <script src="../../../gitbook/gitbook-plugin-edit-link/plugin.js"></script>
        
    
        
        <script src="../../../gitbook/gitbook-plugin-fontsettings/fontsettings.js"></script>
        
    

    </body>
</html>

